package ipdesk.model.report.design.action;


import ipdesk.model.businessInfo.bean.BusinessInfoBean;
import ipdesk.model.businessInfo.dao.BusinessInfoDao;
import ipdesk.model.report.brand.bean.BrandInfoBean;
import ipdesk.model.report.brand.dao.BrandInfoDao;
import ipdesk.model.report.design.bean.DesignInfoBean;
import ipdesk.model.report.design.dao.DesignInfoDao;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.FileService;
import ecg.util.PageUtil;

public class DesignInfoSelectAction extends ActionSupport implements Preparable, ModelDriven<DesignInfoBean>
{
	private DesignInfoBean designInfoBean;
	private int totalCount;
	private Map pageInfoDataMap;
	private String deskCode;
	private List<DesignInfoBean> designList;
	private DesignInfoBean designInfoDetail;
	private DesignInfoBean fileDetail;
	
	private BusinessInfoBean companyInfoDetail;
	private BusinessInfoBean customerInfoDetail;
	
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		
		totalCount = DesignInfoDao.getInstance().getDataTotalCount(designInfoBean);
		
		System.out.println("총 데이터 갯수: " + totalCount);
		pageInfoDataMap = PageUtil.getInstance().getPageInfoData(totalCount, designInfoBean.getPageNumber());
		
		
		if(designInfoBean.getDeskCode() != null || !"".equals(designInfoBean.getDeskCode())){
			
			pageInfoDataMap.put("deskCode", designInfoBean.getDeskCode());
			deskCode = designInfoBean.getDeskCode();
		}
		
		if(designInfoBean.getOrder() != null || !"".equals(designInfoBean.getOrder())){
			
			pageInfoDataMap.put("order", designInfoBean.getOrder());
		}
		
		if(designInfoBean.getDesignSearchKeyword() != null || !"".equals(designInfoBean.getDesignSearchKeyword()))
		{
			pageInfoDataMap.put("designSearchKeyword", designInfoBean.getDesignSearchKeyword());
			pageInfoDataMap.put("designSearchOption", designInfoBean.getDesignSearchOption());
		}
		
		if(designInfoBean.getYear() != null || !"".equals(designInfoBean.getYear())){
			
			pageInfoDataMap.put("year", designInfoBean.getYear());
		}
		
		if(designInfoBean.getState() != null || !"".equals(designInfoBean.getState())){
			
			pageInfoDataMap.put("state", designInfoBean.getState());
		}
		
		// 2. List
		designList = DesignInfoDao.getInstance().getPagingDataListForUser(pageInfoDataMap);
		
		return SUCCESS;
	}
	
	public String executeDetail() throws Exception
	{
		int designSeq = designInfoBean.getDesignSeq();
		
		designInfoDetail = DesignInfoDao.getInstance().getDetailData(designSeq); 
		
		companyInfoDetail = BusinessInfoDao.getInstance().getDetailData(designInfoDetail.getCompanySeq());
		customerInfoDetail = BusinessInfoDao.getInstance().getModifyCustomerData(designInfoDetail.getCustomerSeq());
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE11");
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setDesign(fileDetail.getFile_name());
		}
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE12");
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setApplicationForm(fileDetail.getFile_name());
		}
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designSeq, "FLE13");
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setEvaluationScoreCard(fileDetail.getFile_name());
		}
		
		return SUCCESS;
	}
	
	public String completeDetail() throws Exception {
		designInfoDetail = DesignInfoDao.getInstance().getDetailData(designInfoBean.getDesignSeq());
		
		companyInfoDetail = BusinessInfoDao.getInstance().getDetailData(designInfoDetail.getAgentCompanySeq());
		customerInfoDetail = BusinessInfoDao.getInstance().getModifyCustomerData(designInfoDetail.getAgentCustomerSeq());
		
		fileDetail = DesignInfoDao.getInstance().getFileDetailData(designInfoBean.getDesignSeq(), "FLE10");
		if(fileDetail != null) {
			designInfoDetail.setPath_name(fileDetail.getPath_name());
			designInfoDetail.setNewDesign(fileDetail.getFile_name());
		}
		
		return SUCCESS;
	}
	
	public int getTotalCount()
	{
		return totalCount;
	}

	public void setTotalCount(int totalCount)
	{
		this.totalCount = totalCount;
	}

	public Map getPageInfoDataMap()
	{
		return pageInfoDataMap;
	}

	public void setPageInfoDataMap(Map pageInfoDataMap)
	{
		this.pageInfoDataMap = pageInfoDataMap;
	}

	public List<DesignInfoBean> getDesignList()
	{
		return designList;
	}

	public void setDesignList(List<DesignInfoBean> designList)
	{
		this.designList = designList;
	}

	public DesignInfoBean getDesignInfoDetail()
	{
		return designInfoDetail;
	}

	public void setDesignInfoDetail(DesignInfoBean designInfoDetail)
	{
		this.designInfoDetail = designInfoDetail;
	}
	
	public String getDeskCode()
	{
		return deskCode;
	}

	public void setDeskCode(String deskCode)
	{
		this.deskCode = deskCode;
	}

	public void prepare() throws Exception {
		
		designInfoBean = new DesignInfoBean();
	}

	public DesignInfoBean getModel() {
		
		return designInfoBean;
	}

	public DesignInfoBean getFileDetail() {
		return fileDetail;
	}

	public void setFileDetail(DesignInfoBean fileDetail) {
		this.fileDetail = fileDetail;
	}

	public BusinessInfoBean getCompanyInfoDetail() {
		return companyInfoDetail;
	}

	public BusinessInfoBean getCustomerInfoDetail() {
		return customerInfoDetail;
	}
	
}
